﻿using System;

namespace duia.Canteenpay.Domain
{
    /// <summary>
    /// 数据实体抽象基类
    /// </summary>
    public class BaseEntity : BaseEntityId
    {

        #region 成员变量

        /// <summary>
        /// 实体对象的创建时间
        /// </summary>
        public DateTime CreateTime { get; protected set; } = DateTime.Now;

        /// <summary>
        /// 实体对象的创建人KeyId
        /// </summary>
        public Guid CreateUserKeyId { get; protected set; }

        /// <summary>
        /// 设置创建人
        /// </summary>
        public string CreateUserName { get; protected set; }

        /// <summary>
        /// 实体对象的修改时间
        /// </summary>
        public DateTime? UpdateTime { get; protected set; }

        /// <summary>
        /// 实体对象的修改人KeyId
        /// </summary>
        public Guid? UpdateUserKeyId { get; protected set; }

        /// <summary>
        /// 实体对象的创建人KeyId
        /// </summary>
        public string UpdateUserName { get; protected set; }

        /// <summary>
        /// 实体数据对象物理删除标记
        /// </summary>
        public bool IsDelete { get; protected set; }

        #endregion

        /// <summary>
        /// 设置创建时间
        /// </summary>
        /// <param name="createTime"></param>
        public void SetCreteTime(DateTime createTime)
        {

            if (this.CreateTime == null || DateTime.MinValue.Equals(this.CreateTime))
            {
                this.CreateTime = createTime;
            }
            else
            {
                throw new DomainException("Can not change the create time of an existing object.");
            }
        }


        /// <summary>
        /// 设置创建人ID
        /// </summary>
        /// <param name="createUserKeyId"></param>
        public void SetCreateUserKeyId(Guid createUserKeyId)
        {
            if (this.CreateUserKeyId == null || Guid.Empty.Equals(this.CreateUserKeyId))
            {
                this.CreateUserKeyId = createUserKeyId;
            }
            else
            {
                throw new DomainException("Can not change the create user createUserKeyId of an existing object.");
            }

        }


        /// <summary>
        /// 设置创建人
        /// </summary>
        /// <param name="createUserName"></param>
        public void SetCreateUserName(string createUserName)
        {
            if (!string.IsNullOrEmpty(this.CreateUserName))
            {
                this.CreateUserName = createUserName;
            }
            else
            {
                throw new DomainException("Can not change the create user name of an existing object.");
            }

        }


        public void SetUpdateTime(DateTime? value = null)
        {
            if (value.HasValue)
            {
                this.UpdateTime = value;
            }
            else
            {
                this.UpdateTime = DateTime.Now;
            }

        }

        public void SetUpdateUserKeyId(Guid? value)
        {
            this.UpdateUserKeyId = value;
        }


        /// <summary>
        /// 设置更新人员姓名
        /// </summary>
        /// <param name="value"></param>
        public void SetUpdateUserName(string value)
        {
            this.UpdateUserName = value;
        }

        public void SetIsDelete(bool value)
        {
            this.IsDelete = value;
        }

    }


    /// <summary>
    /// 实体主键，全部实体对象都存在
    /// </summary>
    public abstract class BaseEntityId
    {
        /// <summary>
        /// 全局唯一主键标识符
        /// </summary>
        public Guid KeyId { get; protected set; } = Guid.NewGuid();

        /// <summary>
        /// 设置主键
        /// </summary>
        /// <param name="keyId"></param>
        public void SetKeyId(Guid keyId)
        {
            this.KeyId = keyId;
        }

    }
}
